package com.uhf.uhf.kernel

import android.view.KeyEvent
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.lifecycleScope
import com.logic.idevice.IUHFScan
import com.logic.idevice.impl.di.ScanDi
import com.sinopec.basemodule.base.BaseActivity
import com.sinopec.basemodule.constants.DeviceEnum
import com.sinopec.basemodule.utils.ToastUtils
import io.reactivex.disposables.Disposable


/**
 * create by lwj at 2023/4/26
 * 旨在 自动注入和获取 uhf的控制器 用于扫码页面
 */
inline fun BaseActivity<*>.uhfKernel(
    uhfCall: UhfKernel.() -> Unit = {}
): UhfKernel {
    val call = UhfKernel(this)
    uhfCall.invoke(call)
    return call
}

private class KeyEventDisposable(
    val activity: BaseActivity<*>,
    val event: KeyEvent.Callback,
) : Disposable {

    private var disposed = true

    /**
     * Dispose the resource, the operation should be idempotent.
     */
    override fun dispose() {
        disposed = false
    }

    /**
     * Returns true if this resource has been disposed.
     * @return true if this resource has been disposed
     */
    override fun isDisposed(): Boolean {
        return disposed
    }

}

//IRefreshSpeed, IRefreshData,
//    IScanStatus
class UhfKernel(
    val activity: BaseActivity<*>,
    var speed: IUHFScan.IRefreshSpeed = IUHFScan.IRefreshSpeed { _, _, _ -> },
    var data: IUHFScan.IRefreshData = IUHFScan.IRefreshData { },
    var state: IUHFScan.IScanStatus = IUHFScan.IScanStatus { },
) : IUHFScan.IRefreshSpeed, IUHFScan.IRefreshData,
    IUHFScan.IScanStatus {

    val uhfKeyEventCall: KeyEvent.Callback by lazy {
        object : KeyEvent.Callback {
            override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                return when (keyCode) {
                    KeyEvent.KEYCODE_BACK -> {
                        activity.finish()
                        true
                    }
                    KeyEvent.KEYCODE_F4 -> {
                        //如果当前界面正在上传，return掉
                        scan.startOrStop(true)
                        true
                    }
                    else -> {
                        false
                    }
                }
            }

            override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean {
                return false
            }

            override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                return when (keyCode) {
                    KeyEvent.KEYCODE_F4 -> {
                        //手持按键点击事件
                        scan.startOrStop(false)
                        true
                    }
                    else -> {
                        false
                    }
                }
            }

            override fun onKeyMultiple(keyCode: Int, count: Int, event: KeyEvent?): Boolean {
                return false
            }
        }
    }


    val scan by lazy {
        ScanDi.inject(this, this, this, activity.lifecycleScope)
    }

    init {
        activity.lifecycle.addObserver(LifecycleEventObserver { source, event ->
            if (event == Lifecycle.Event.ON_CREATE) {
                if (!DeviceEnum.isHandDevice()) {
                    ToastUtils.showToast("请使用乐橘手持设备，运行该app！")
                    activity.finish()
                } else {
                    scan.init(activity)
                }
            }
        })
    }

    override fun onRefreshSpeed(count: Int, rate: Int, time: Long) {
        speed.onRefreshSpeed(count, rate, time)
    }

    override fun onRefreshData(codes: List<String>) {
        data.onRefreshData(codes)
    }

    override fun onScanStatusChange(isOpen: Boolean) {
        state.onScanStatusChange(isOpen)
    }
}